define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'learn/chapter/index' + location.search,
                    add_url: 'learn/chapter/add',
                    addcourse_url: 'learn/chapter/addcourse'+ location.search,
                    edit_url: 'learn/chapter/edit',
                    del_url: 'learn/chapter/del',
                    multi_url: 'learn/chapter/multi',
                    table: 'learn_course_chapter',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate:'LIKE'},
                        {field: 'file', title: __('File'), operate:false},
                        {field: 'score', title: __('Score'), operate:'>='},
                        {field: 'edu_learn_course_id', title: __('Course_id')},
                        {field: 'course.name', title: __('Course_name'), operate:'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'image', title: __('Image'), operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'introduce', title: __('Introduce'), operate:'LIKE'},

                        {field: 'operate', title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

                // Layer.alert(JSON.stringify(table.bootstrapTable('getSelections')));
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        addcourse: function () {
            Table.api.init({
                extend: {
                    addcourse_url: 'learn/chapter/addcourse'+ location.search,
                    table: 'chapter_addcourse'
                }
            });

            var table1 = $('#table1');

            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.addcourse_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Course.name'),operate:'LIKE'},
                        {field: 'origin_price', title: __('Course.origin_price'), operate:'BETWEEN'},
                        {field: 'price', title: __('Course.price'), operate:'BETWEEN'},
                        {field: 'image', title: __('Course.image'), operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('Course.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'is_chat', title: __('Course.is_chat'), searchList: {"0":__('Course.is_chat 0'),"1":__('Course.is_chat 1')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Course.status'), searchList: {"0":__('Course.status 0'),"1":__('Course.status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'),
                            table: table1,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'click',
                                    title: __('选择课程'),
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-leaf',
                                    click: function (data,row) {
                                        console.log('data',data);
                                        console.log('row',row);
                                        confirmf(row.id);
                                        // Layer.alert("点击按钮执行的事件");
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true
            });
            // 为表格绑定事件
            Table.api.bindevent(table1);
            // 获取选中项
            var confirmf = function(course_id){
                Layer.confirm("确定绑定该课程吗",
                    {
                        title: '章节绑定课程',
                        btn: ["确定", "取消"],
                        yes: function (index, layero) {
                            submitExportForm(course_id,index, layero);
                            return false;
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                            return false;
                        },
                    });
            }
            var submitExportForm = function (course_id,index, layero) {
                var data = {
                    ids: $('#task_id').val(),
                    task_id: course_id
                };
                Fast.api.ajax({
                    url: Fast.api.fixurl('learn/chapter/bindcourse'),
                    data: data,
                    pk: 'id'
                }, function (res) {
                    Layer.closeAll();
                    Fast.api.close(layero.selector);
                });
            };
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

            }
        }

    };
    return Controller;
});